package com.cat.net.network.kafka.serializer;

import com.cat.net.network.base.Packet;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.UnpooledByteBufAllocator;
import org.apache.kafka.common.serialization.Serializer;


/**
 * Packet序列化字节流
 * @author Jeremy
 */
public class KafkaPacketSerializer implements Serializer<Packet> {

    @Override
    public byte[] serialize(String s, Packet packet) {
        ByteBuf buffer = UnpooledByteBufAllocator.DEFAULT.heapBuffer();
        buffer.writeInt(packet.cmd());
        buffer.writeInt(packet.seq());
        buffer.writeBytes(packet.data());

        int length = buffer.readableBytes();
        byte[] data = new byte[length];
        buffer.readBytes(data);
        return data;
    }
}
